package com.reworkssm.mybatisdemo.controller;

import com.reworkssm.mybatisdemo.domain.*;
import com.reworkssm.mybatisdemo.domain.ServerResponse;
import com.reworkssm.mybatisdemo.service.CatalogService;
import com.reworkssm.mybatisdemo.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/catalog/")
public class CatalogController {
    @Autowired
    private ICategoryService icategoryService;

    @Autowired
    private CatalogService catalogService;
    //20200606修改
    /**
     * 新增品类
     *
     * @param session
     * @param categoryName
     * @param parentId
     * @return
     */
    @RequestMapping(value = "add_category.do")
    @ResponseBody
    public ServerResponse addCategory(HttpSession session, String categoryName, @RequestParam(value = "parentId", defaultValue = "0") Integer parentId) {

        User user = (User) session.getAttribute(Const.CURRENT_USER);

        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登陆");
        }

        if (user.getRole() == Const.Role.ROLE_ADMIN) {
            //是管理员，插入品类信息
            return icategoryService.addCategory(categoryName, parentId);
        }

        return ServerResponse.createByErrorMessage("用户无权限");
    }

    /**
     * 修改品类名称
     *
     * @param session
     * @param categoryNewName
     * @param categoryId
     * @return
     */
    @RequestMapping(value = "set_category_name.do")
    @ResponseBody
    public ServerResponse setCategoryName(HttpSession session, String categoryNewName, Integer categoryId) {

        User user = (User) session.getAttribute(Const.CURRENT_USER);

        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登陆");
        }

        if (user.getRole() == Const.Role.ROLE_ADMIN) {
            //是管理员，修改品类的名称
            return icategoryService.updateCategoryName(categoryNewName, categoryId);
        }

        return ServerResponse.createByErrorMessage("用户无权限");

    }

    /**
     * 通过id获取品类信息
     *
     * @param session
     * @param categoryId
     * @return
     */
    @RequestMapping(value = "get_category.do")
    @ResponseBody
    public ServerResponse<List<Category>> getCategoryChildInfoByCategoryId(HttpSession session, @RequestParam(value = "categoryId", defaultValue = "0") Integer categoryId) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);

        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登陆");
        }

        if (user.getRole() == Const.Role.ROLE_ADMIN) {
            //是管理员，获取子节点
            return icategoryService.getCategoryChildInfoByCategoryId(categoryId);
        }

        return ServerResponse.createByErrorMessage("用户无权限");


    }

    @RequestMapping(value = "get_deep_category.do")
    @ResponseBody
    public ServerResponse getCateforyAndDescendantIdByCategoryId(HttpSession session, Integer categoryId) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);

        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登陆");
        }

        if (user.getRole() == Const.Role.ROLE_ADMIN) {
            //是管理员，获取当前节点id及递归子节点的categoryId
            return icategoryService.getCateforyAndDescendantIdByCategoryId(categoryId);

        }

        return ServerResponse.createByErrorMessage("用户无权限");


    }

    @GetMapping("viewCategory")
    public String viewCategory(String categoryId, Model model) {
        if (categoryId != null) {
            List<Product> productList = catalogService.getProductListByCategory(categoryId);
            Category category = catalogService.getCategory(categoryId);
            model.addAttribute("productList", productList);
            model.addAttribute("category", category);
        }
        return "catalog/Category";
    }

    @GetMapping("viewmain")
    public String viewMain() {
        return "catalog/Main";
    }

    @GetMapping("main")
    public String viewMain1() {
        return "catalog/Main";
    }

    @GetMapping("viewProduct")
    public String viewProduct(String productId, Model model) {
        if (productId != null) {
            List<Item> itemList = catalogService.getItemListByProduct(productId);
            Product product = catalogService.getProduct(productId);
            model.addAttribute("itemList",itemList);
            model.addAttribute("product",product);
        }
        return "catalog/Product";
    }
    @GetMapping("viewItem")
    public String viewItem(String itemId, Model model) {
        Item item = catalogService.getItem(itemId);
        Product product = item.getProduct();
        processProductDescription(product);
        model.addAttribute("item",item);
        model.addAttribute("product",product);
        return "catalog/Item";
    }
    @PostMapping("searchProducts")
    public String searchProducts(String keyword,Model model) {
        if (keyword != null && keyword.length() >= 1) {
            List<Product> productList = catalogService.searchProductList(keyword.toLowerCase());
            processProductDescription(productList);
            model.addAttribute("productList",productList);
            return "catalog/SearchProducts";
        } else {
            //setMessage("Please enter a keyword to search for, then press the search button.");
            return "common/error";
        }
    }

    private void processProductDescription(Product product){
        String[] temp = product.getDescription().split("\"");
        product.setDescriptionImage(temp[1]);
        product.setDescriptionText(temp[2].substring(1));
    }

    private void processProductDescription(List<Product> products){
        for(Product product : products){
            processProductDescription(product);
        }
    }

}